import { createRouter, createWebHistory } from 'vue-router'
import homeView from '@/views/home/home.vue'
import indexView from '@/views/home/index.vue'
import catView from '@/views/home/cat.vue'
import gwcView from '@/views/home/gwc.vue'
import myView from '@/views/home/my.vue'
import searchView from '@/views/home/search.vue'
import loginView from '@/views/login/index.vue'
import registerView from '@/views/register/index.vue'
import detailView from '@/views/detail/index.vue'
import orderView from '@/views/order/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/index' },
    {
      path: '/home', name: 'home', component: homeView, children: [
        { path: 'index', name: 'index', component: indexView, },
        { path: 'cat', name: 'cat', component: catView, },
        { path: 'gwc', name: 'gwc', component: gwcView, },
        { path: 'my', name: 'my', component: myView, },
        { path: 'search', name: 'search', component: searchView, },
      ]
    },
    { path: '/login', name: 'login', component: loginView, },
    { path: '/register', name: 'register', component: registerView, },
    { path: '/detail/:goods_id', name: 'detail', component: detailView, },
    { path: '/order/:orderId', name: 'order', component: orderView, },

  ],
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  if (to.fullPath == '/home/gwc' || to.fullPath == '/home/my') {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
  next()
})
export default router
